package com.zhongshengnetwork.rightbe.lang.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
//import android.support.annotation.Nullable;
//import android.support.v4.app.Fragment;
//import android.support.v4.app.FragmentPagerAdapter;
//import android.support.v4.view.PagerAdapter;
//import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.MainActivity;
import com.zhongshengnetwork.rightbe.PageOneFragment;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BaseAnalyticsFragment;
import com.zhongshengnetwork.rightbe.wzt.view.MyWZTFragment;
import com.zhongshengnetwork.rightbe.wzt.view.WZTFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luojingxing on 2018/6/13.
 */

public class MeFragment extends BaseAnalyticsFragment implements View.OnClickListener{

    private View rootView;
    private ViewPager mPaper;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments = null;
    private TextView tv_lang,tv_wzt,tv_life,tv_collect,tv_like,tv_fan,tv_care,tv_honor,tv_main;
    private LinearLayout tv_lang_line,tv_wzt_line,tv_life_line,tv_collect_line,tv_like_line,tv_fan_line,tv_care_line,tv_honor_line,tv_main_line;
    public MainActivity mainActivity;
    private UserLangFragment userLangFragment;
    private MyWZTFragment wztFragment;
    private UserLifeLangFragment userLifeLangFragment;
    private CollectFragment collectFragment;
    private LikeLangFragment likeLangFragment;
    private CareOrFansFragment careFragment;
    private CareOrFansFragment fansFragment;
    private UserFragment userFragment;
    HonorLangFragment honorLangFragment;
    private int currentPosition = 0;

    public void updateMyInfo(){

        if (mFragments != null && mFragments.size() > 0){
            userFragment = (UserFragment)mFragments.get(0);
        }

        if (userFragment != null && userFragment.loaded == true){
            userFragment.getUserInfo();
        }
    }

    public void updateSignText(){
        if (mFragments != null && mFragments.size() > 0){
            userFragment = (UserFragment)mFragments.get(0);
        }
        if (userFragment != null){
            userFragment.updateSignText();
        }
    }

    //登录后重新更新数据
    public void updateUserinfo(){
        if (mFragments != null && mFragments.size() > 0){
            userFragment = (UserFragment)mFragments.get(0);
        }
        if (userFragment != null){
            userFragment.updateUserInfo();
        }
        if (userLangFragment != null){
            userLangFragment.getData(true);
        }
    }

    //更新阅读时长
    public void updateReadTimeText(){
        if (mFragments != null && mFragments.size() > 0){
            userFragment = (UserFragment)mFragments.get(0);
        }
        if (userFragment != null){
            userFragment.updateReadTimeText();
        }
    }

    public void stFragment(int index){
        if (mPaper == null){
            return;
        }
        resetColor();
        setSelect(index);
        mPaper.setCurrentItem(index);
        getFirstData(index, false);
    }

    public void getFirstData(){
        getFirstData(currentPosition, true);
    }

    private void getFirstData(int index, boolean isFirst){
        if (index == 1){
            if (userLangFragment != null){
                userLangFragment.getFirstData(isFirst);
            }
        }else if (index == 2){
            if (wztFragment != null){
                wztFragment.getFirstData(isFirst);
            }
        }else if (index == 3){
            if (userLifeLangFragment != null){
                userLifeLangFragment.getFirstData(isFirst);
            }
        }else if (index == 4){
            if (collectFragment != null){
                collectFragment.getData();
            }
        }else if(index == 5){
            if (likeLangFragment != null){
                likeLangFragment.getFirstData(isFirst);
            }
        }else if (index == 6){
            if (fansFragment != null){
                fansFragment.getFirstData(isFirst);
            }
        }else if (index == 7){
            if (careFragment != null){
                careFragment.getFirstData(isFirst);
            }
        }else if (index == 8){
            if (honorLangFragment != null){
                honorLangFragment.getFirstData(isFirst);
            }
        }
    }

    public void updateMyLang(){
        if (mPaper == null){
            return;
        }
        resetColor();
        setSelect(1);
        mPaper.setCurrentItem(1);
        if (mFragments != null && mFragments.size() > 1){
            userLangFragment = (UserLangFragment) mFragments.get(1);
        }
        if (userLangFragment != null){
            userLangFragment.getData(true);
        }
        if (userLifeLangFragment != null){
            userLifeLangFragment.getData(true);
        }
    }

    public void updateMyWZT(){
        if (mPaper == null){
            return;
        }
        resetColor();
        setSelect(2);
        mPaper.setCurrentItem(2);
        if (wztFragment != null){
            wztFragment.updateData();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //System.out.print("首页界面创建\n");
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.mefragment, container,false);
            initLayout();
            if (mAdapter == null){
                mAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
                    @Override
                    public int getCount() {
                        return mFragments.size();
                    }

                    @Override
                    public Fragment getItem(int arg0) {
                        return mFragments.get(arg0);
                    }


                };
            }
            mPaper.setAdapter(mAdapter);
            mPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                private int currentIndex = 0;

                @Override
                public void onPageSelected(int position) {
                    resetColor();
                    setSelect(position);
                    currentIndex = position;
                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {

                }
            });

            LinearLayout category_layout = (LinearLayout) rootView.findViewById(R.id.category_layout);
            category_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

        }else {
            //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }


    private void setSelect(int position){
        if (tv_lang == null || tv_lang_line == null || tv_wzt == null || tv_wzt_line == null || tv_life == null || tv_life_line == null
                || tv_collect == null || tv_collect_line == null || tv_fan == null || tv_fan_line == null
                || tv_care == null || tv_care_line == null || tv_honor == null || tv_honor_line == null
                || tv_main == null || tv_main_line == null){
            return;
        }
        currentPosition = position;
        switch (position) {
            case 0:
                updateSignText();
                tv_main.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_main_line.setVisibility(View.VISIBLE);
                break;
            case 1://FC6E51
                userLangFragment.getFirstData(false);
                tv_lang.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_lang_line.setVisibility(View.VISIBLE);
                break;
            case 2:
                wztFragment.getFirstData(false);
                tv_wzt.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_wzt_line.setVisibility(View.VISIBLE);
                break;
            case 3:
                userLifeLangFragment.getFirstData(false);
                tv_life.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_life_line.setVisibility(View.VISIBLE);
                break;

            case 4:
                collectFragment.getData();
                tv_collect.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_collect_line.setVisibility(View.VISIBLE);
                break;
            case 5:
                likeLangFragment.getFirstData(false);
                tv_like.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_like_line.setVisibility(View.VISIBLE);
                break;

            case 6:
                fansFragment.getFirstData(false);
                tv_fan.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_fan_line.setVisibility(View.VISIBLE);
                break;
            case 7:
                careFragment.getFirstData(false);
                tv_care.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_care_line.setVisibility(View.VISIBLE);
                break;
            case 8:
                honorLangFragment.getFirstData(false);
                tv_honor.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_honor_line.setVisibility(View.VISIBLE);
                break;
            default:
                tv_lang.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_lang_line.setVisibility(View.VISIBLE);
                mPaper.setCurrentItem(1);
                break;
        }
    }

    /**
     * 初始化控件
     */
    public void initLayout(){
        tv_lang = (TextView)rootView.findViewById(R.id.tv_lang);
        tv_lang_line = (LinearLayout) rootView.findViewById(R.id.tv_lang_line);

        tv_wzt = (TextView)rootView.findViewById(R.id.tv_wzt);
        tv_wzt_line = (LinearLayout) rootView.findViewById(R.id.tv_wzt_line);

        tv_life = (TextView)rootView.findViewById(R.id.tv_life);
        tv_life_line = (LinearLayout) rootView.findViewById(R.id.tv_life_line);
        tv_collect = (TextView)rootView.findViewById(R.id.tv_collect);
        tv_collect_line = (LinearLayout) rootView.findViewById(R.id.tv_collect_line);
        tv_like = (TextView)rootView.findViewById(R.id.tv_like);
        tv_like_line = (LinearLayout) rootView.findViewById(R.id.tv_like_line);

        tv_fan = (TextView)rootView.findViewById(R.id.tv_fan);
        tv_fan_line = (LinearLayout) rootView.findViewById(R.id.tv_fan_line);
        tv_care = (TextView)rootView.findViewById(R.id.tv_care);
        tv_care_line = (LinearLayout)rootView.findViewById(R.id.tv_care_line);
        tv_honor = (TextView)rootView.findViewById(R.id.tv_honor);
        tv_honor_line = (LinearLayout)rootView.findViewById(R.id.tv_honor_line);
        tv_main = (TextView)rootView.findViewById(R.id.tv_main);
        tv_main_line = (LinearLayout)rootView.findViewById(R.id.tv_main_line);
        mPaper = (ViewPager)rootView.findViewById(R.id.view_pager);

        tv_lang.setOnClickListener(this);
        tv_wzt.setOnClickListener(this);
        tv_life.setOnClickListener(this);
        tv_collect.setOnClickListener(this);
        tv_like.setOnClickListener(this);
        tv_fan.setOnClickListener(this);
        tv_care.setOnClickListener(this);
        tv_honor.setOnClickListener(this);
        tv_main.setOnClickListener(this);

        resetColor();
        setSelect(0);

        if (mFragments == null){
            mFragments = new ArrayList<Fragment>();
            userFragment = new UserFragment();
            mFragments.add(userFragment);

            userLangFragment = new UserLangFragment();
            userLangFragment.mainActivity = mainActivity;
            mFragments.add(userLangFragment);

            wztFragment = new MyWZTFragment();
            mFragments.add(wztFragment);

            /*
            commentFragment = new CommentFragment();
            commentFragment.type = 0;
            //commentFragment.userid = CustomApplication.loginModel.getUid();
            commentFragment.isNeedLogin = true;
            mFragments.add(commentFragment);
            */
            userLifeLangFragment = new UserLifeLangFragment();
            userLifeLangFragment.mainActivity = mainActivity;
            mFragments.add(userLifeLangFragment);

            collectFragment = new CollectFragment();
            mFragments.add(collectFragment);

            likeLangFragment = new LikeLangFragment();
            mFragments.add(likeLangFragment);

            fansFragment = new CareOrFansFragment();
            fansFragment.type = 1;
            mFragments.add(fansFragment);

            careFragment = new CareOrFansFragment();
            careFragment.type = 0;
            mFragments.add(careFragment);

            honorLangFragment = new HonorLangFragment();
            mFragments.add(honorLangFragment);
        }

    }

    public void resetColor(){
        if (tv_lang == null || tv_lang_line == null || tv_wzt == null || tv_wzt_line == null || tv_life == null || tv_life_line == null
                || tv_collect == null || tv_collect_line == null || tv_fan == null || tv_fan_line == null
                || tv_care == null || tv_care_line == null || tv_honor == null || tv_honor_line == null
                || tv_main == null || tv_main_line == null){
            return;
        }

        tv_lang.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_lang_line.setVisibility(View.INVISIBLE);

        tv_wzt.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_wzt_line.setVisibility(View.INVISIBLE);

        tv_life.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_life_line.setVisibility(View.INVISIBLE);
        tv_collect.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_collect_line.setVisibility(View.INVISIBLE);

        tv_like.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_like_line.setVisibility(View.INVISIBLE);

        tv_fan.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_fan_line.setVisibility(View.INVISIBLE);
        tv_care.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_care_line.setVisibility(View.INVISIBLE);
        tv_honor.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_honor_line.setVisibility(View.INVISIBLE);
        tv_main.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_main_line.setVisibility(View.INVISIBLE);
    }
    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.tv_main:
                resetColor();
                setSelect(0);
                mPaper.setCurrentItem(0);
                break;
            case R.id.tv_lang:
                resetColor();
                setSelect(1);
                mPaper.setCurrentItem(1);
                break;
            case R.id.tv_wzt:
                resetColor();
                setSelect(2);
                mPaper.setCurrentItem(2);
                break;
            case R.id.tv_life:
                resetColor();
                setSelect(3);
                mPaper.setCurrentItem(3);
                break;

            case R.id.tv_collect:
                resetColor();
                setSelect(4);
                mPaper.setCurrentItem(4);
                break;
            case R.id.tv_like:
                resetColor();
                setSelect(5);
                mPaper.setCurrentItem(5);
                break;
            case R.id.tv_fan:
                resetColor();
                setSelect(6);
                mPaper.setCurrentItem(6);
                break;
            case R.id.tv_care:
                resetColor();
                setSelect(7);
                mPaper.setCurrentItem(7);
                break;
            case R.id.tv_honor:
                resetColor();
                setSelect(8);
                mPaper.setCurrentItem(8);
                break;

            default:
                resetColor();
                setSelect(1);
                mPaper.setCurrentItem(1);
                break;
        }
    }
    /**
     * ViewPager适配器
     */
    public class MyPagerAdapter extends PagerAdapter {
        public List<Activity> mListViews;

        public MyPagerAdapter(List<Activity> mListViews) {
            this.mListViews = mListViews;
        }


        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }



        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }

}

